/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Params;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*;


/**

 */
public class ThreadConnectionSocketChat extends Thread implements Runnable{
    public SocketListener socketListener;
    public Socket connectionSocket ;
    public BufferedReader bufferedReader;//doc chuoi tu mang goi ve
    public PrintWriter bufferedWriter;//ghi chuoi xuat len mang
    
    public ThreadConnectionSocketChat(Socket socket,SocketListener socketListener){
        this.connectionSocket = socket;
        this.socketListener = socketListener;
        try {
            this.bufferedReader = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            this.bufferedWriter = new PrintWriter(connectionSocket.getOutputStream(),true);
        } catch (Exception e) {
            System.out.println(e);
        }
        start();//goi thread chay
    }
    public void GiaiPhong() throws IOException
    {
        connectionSocket.close();
    }
    
    @Override
    public void run(){
        while(true){
            try {
                    ChatMessage message = new ChatMessage();
                    message.setType(1);
                    message.setName(String.valueOf(this.connectionSocket.getPort()));
                    message.setMessage(this.bufferedReader.readLine().toString());
                    socketListener.onRead(message);
            } catch (Exception e) {
                System.out.println(e);
            }
            //lenh nay thong bao cho scheduler biet(dag ranh) de thuc thi 1 thread khac
            Thread.yield();
        }
    }
    
}
